package com.waiting.reflection;

import java.lang.reflect.Method;

/**
 * @desctiption 反射中常用的Method类
 * @Author bjweitingting
 * @date 2018/5/11
 **/
public class MethodDemo1 {
    public static void main(String[] args) {
        ReflectionClass reflectionClass = new ReflectionClass();
        Class type = reflectionClass.getClass();
        Method[] methods = type.getMethods();
        for (Method oneMethod : methods) {
            System.out.println(oneMethod.getName());
        }
        //获取指定方法名和方法传参的函数
        try {
            Method method = type.getMethod("setParms", String.class);
            //调用非静态方法
            method.invoke(reflectionClass, "test");

            //以下两种方式都可以实现调用静态方法的调用
            Method methodStatic = type.getMethod("staticFunction");
            System.out.println(methodStatic.getName());
            methodStatic.invoke(reflectionClass);
            methodStatic.invoke(ReflectionClass.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
